﻿/******************************************************************************
文件名称    :   MySqlChemistryImmunityDataProvider.cs
创建日期    :   2011/5/29 10:51:33
功能描述    :   ChemistryImmunity实体数据提供程序MySql实现类
修改日志    :   
    2011-05-29 10:51  马利
*******************************************************************************/

using System;
using System.Collections.Generic;
using System.Text;
using System.Data.Common;
using System.Data;
using Dare.Utilities.Data;
using Stone.Data.Providers;
using Stone.Components.Entities;

namespace Stone.Data.MySql.Providers
{
    /// <summary>
    /// ChemistryImmunity实体数据提供程序MySql实现类
    /// </summary>
    public class MySqlChemistryImmunityDataProvider : ChemistryImmunityDataProvider
    {
        public MySqlChemistryImmunityDataProvider()
            : base()
        {
        }

        public override void Insert(TransactionManager manager, ChemistryImmunity entity)
        {
            Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
            {
                /*------------------------------数据库SQL操作开始----------------------------------*/

                //插入SQL语句
                cmd.CommandText = "insert into tb_chemistry_immunity(IL2, IL6, IL8, IL10, TNF, CRP, neomycin, comment) values(?IL2, ?IL6, ?IL8, ?IL10, ?TNF, ?CRP, ?neomycin, ?comment);" +
                    "select id, time_stamp from tb_chemistry_immunity where id = last_insert_id();";
                GenericDbHelper.AddParameter(mgr, cmd, "?IL2", DbType.Decimal).Value = entity.Il2 == null ? DBNull.Value : (object)entity.Il2.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?IL6", DbType.Decimal).Value = entity.Il6 == null ? DBNull.Value : (object)entity.Il6.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?IL8", DbType.Decimal).Value = entity.Il8 == null ? DBNull.Value : (object)entity.Il8.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?IL10", DbType.Decimal).Value = entity.Il10 == null ? DBNull.Value : (object)entity.Il10.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?TNF", DbType.Decimal).Value = entity.Tnf == null ? DBNull.Value : (object)entity.Tnf.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?CRP", DbType.Decimal).Value = entity.Crp == null ? DBNull.Value : (object)entity.Crp.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?neomycin", DbType.Decimal).Value = entity.Neomycin == null ? DBNull.Value : (object)entity.Neomycin.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?comment", DbType.String).Value = entity.Comment == null ? DBNull.Value : (object)entity.Comment;

                //执行数据库操作,并获得主键和更新时间戳
                using (IDataReader reader = cmd.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        entity.Id = reader.GetInt32(0);
                        entity.TimeStamp = reader.GetDateTime(1);
                        entity.EntityState = Stone.Components.EntityState.Saved;
                    }
                }

                //插入关联
                InsertRelations(mgr, cmd, entity);

                /*------------------------------数据库SQL操作结束----------------------------------*/
            });
        }

        public override void Import(TransactionManager manager, IEnumerable<ChemistryImmunity> entities)
        {
            Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
            {
                /*------------------------------数据库SQL操作开始----------------------------------*/

                //插入SQL语句
                cmd.CommandText = "insert into tb_chemistry_immunity(id, IL2, IL6, IL8, IL10, TNF, CRP, neomycin, comment, time_stamp) values(?id, ?IL2, ?IL6, ?IL8, ?IL10, ?TNF, ?CRP, ?neomycin, ?comment, ?time_stamp);";
                GenericDbHelper.AddParameter(mgr, cmd, "?id", DbType.Int64);
                GenericDbHelper.AddParameter(mgr, cmd, "?IL2", DbType.Decimal);
                GenericDbHelper.AddParameter(mgr, cmd, "?IL6", DbType.Decimal);
                GenericDbHelper.AddParameter(mgr, cmd, "?IL8", DbType.Decimal);
                GenericDbHelper.AddParameter(mgr, cmd, "?IL10", DbType.Decimal);
                GenericDbHelper.AddParameter(mgr, cmd, "?TNF", DbType.Decimal);
                GenericDbHelper.AddParameter(mgr, cmd, "?CRP", DbType.Decimal);
                GenericDbHelper.AddParameter(mgr, cmd, "?neomycin", DbType.Decimal);
                GenericDbHelper.AddParameter(mgr, cmd, "?comment", DbType.String);
                GenericDbHelper.AddParameter(mgr, cmd, "?time_stamp", DbType.DateTime);

                //执行数据库操作
                foreach (ChemistryImmunity entity in entities)
                {
                    int i = 0;
                    cmd.Parameters[i++].Value = entity.Id;
                    cmd.Parameters[i++].Value = entity.Il2 == null ? DBNull.Value : (object)entity.Il2.Value;
                    cmd.Parameters[i++].Value = entity.Il6 == null ? DBNull.Value : (object)entity.Il6.Value;
                    cmd.Parameters[i++].Value = entity.Il8 == null ? DBNull.Value : (object)entity.Il8.Value;
                    cmd.Parameters[i++].Value = entity.Il10 == null ? DBNull.Value : (object)entity.Il10.Value;
                    cmd.Parameters[i++].Value = entity.Tnf == null ? DBNull.Value : (object)entity.Tnf.Value;
                    cmd.Parameters[i++].Value = entity.Crp == null ? DBNull.Value : (object)entity.Crp.Value;
                    cmd.Parameters[i++].Value = entity.Neomycin == null ? DBNull.Value : (object)entity.Neomycin.Value;
                    cmd.Parameters[i++].Value = entity.Comment == null ? DBNull.Value : (object)entity.Comment;
                    cmd.Parameters[i++].Value = entity.TimeStamp;
                    cmd.ExecuteNonQuery();
                }

                //插入关联
                InsertRelations(mgr, cmd, entities);

                /*------------------------------数据库SQL操作结束----------------------------------*/
            });
        }

        public override void Update(TransactionManager manager, ChemistryImmunity entity)
        {
            Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
            {
                /*------------------------------数据库SQL操作开始----------------------------------*/
                //更新SQL语句
                cmd.CommandText = "update tb_chemistry_immunity set IL2 = ?IL2, IL6 = ?IL6, IL8 = ?IL8, IL10 = ?IL10, TNF = ?TNF, CRP = ?CRP, neomycin = ?neomycin, comment = ?comment where id = ?id;"
                        + "select time_stamp from tb_chemistry_immunity where id = ?id;";
                GenericDbHelper.AddParameter(mgr, cmd, "?IL2", DbType.Decimal).Value = entity.Il2 == null ? DBNull.Value : (object)entity.Il2.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?IL6", DbType.Decimal).Value = entity.Il6 == null ? DBNull.Value : (object)entity.Il6.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?IL8", DbType.Decimal).Value = entity.Il8 == null ? DBNull.Value : (object)entity.Il8.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?IL10", DbType.Decimal).Value = entity.Il10 == null ? DBNull.Value : (object)entity.Il10.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?TNF", DbType.Decimal).Value = entity.Tnf == null ? DBNull.Value : (object)entity.Tnf.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?CRP", DbType.Decimal).Value = entity.Crp == null ? DBNull.Value : (object)entity.Crp.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?neomycin", DbType.Decimal).Value = entity.Neomycin == null ? DBNull.Value : (object)entity.Neomycin.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?comment", DbType.String).Value = entity.Comment == null ? DBNull.Value : (object)entity.Comment;
                GenericDbHelper.AddParameter(mgr, cmd, "?id", DbType.Int64).Value = entity.Id;

                //执行数据库操作,并获得更新时间戳
                using (IDataReader reader = cmd.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        entity.TimeStamp = reader.GetDateTime(0);
                        entity.EntityState = Stone.Components.EntityState.Saved;
                    }
                }

                //更新关联
                UpdateRelations(mgr, cmd, entity);

                /*------------------------------数据库SQL操作结束----------------------------------*/
            });
        }

        public override void Delete(TransactionManager manager, long key)
        {
            Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
            {
                /*------------------------------数据库SQL操作开始----------------------------------*/

                //先删除关联
                DeleteRelations(mgr, cmd, key);

                //删除SQL语句
                cmd.CommandText = "delete from tb_chemistry_immunity where id = ?id;";
                GenericDbHelper.AddParameter(mgr, cmd, "?id", DbType.Int64).Value = key;

                //执行
                cmd.ExecuteNonQuery();
                /*------------------------------数据库SQL操作结束----------------------------------*/
            });
        }

        public override void DeleteAll(TransactionManager manager, string whereClause)
        {
            Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
            {
                /*------------------------------具体SQL操作开始----------------------------------*/

                //先删除关联
                DeleteRelations(mgr, cmd, whereClause);

                //删除SQL语句
                cmd.CommandText = String.Format("delete from tb_chemistry_immunity {0}",
                     ProcessWhereClause(whereClause));

                //执行
                cmd.ExecuteNonQuery();

                /*------------------------------具体SQL操作结束----------------------------------*/
            });
        }

        public override ChemistryImmunity Get(TransactionManager manager, long key)
        {
            ChemistryImmunity entity = null;
            Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
            {
                /*------------------------------数据库SQL操作开始----------------------------------*/
                //获取SQL语句
                cmd.CommandText = "select * from tb_chemistry_immunity where id = ?id";
                GenericDbHelper.AddParameter(mgr, cmd, "?id", DbType.Int64).Value = key;

                //执行获取实体
                using (IDataReader reader = cmd.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        entity = Fill(reader, null);
                    }
                }

                //获取关联
                if (entity != null)
                {
                    RetrieveRelations(mgr, cmd, entity);
                    entity.EntityState = Stone.Components.EntityState.Saved;
                }

                /*------------------------------数据库SQL操作结束----------------------------------*/
            });

            return entity;
        }

        public override List<ChemistryImmunity> GetPaged(TransactionManager manager, string whereClause, string orderBy, int pageIndex, int pageSize)
        {
            List<ChemistryImmunity> list = new List<ChemistryImmunity>(pageSize > 0 ? Math.Min(pageSize, 256) : 32);
            Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
            {
                /*------------------------------数据库SQL操作开始----------------------------------*/
                //获取SQL语句
                cmd.CommandText = String.Format("select * from tb_chemistry_immunity {0} {1} {2}",
                    ProcessWhereClause(whereClause),
                    ProcessOrderBy(orderBy),
                    ProcessPaging(pageIndex, pageSize));

                //执行获取实体集
                using (IDataReader reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        list.Add(Fill(reader, null));
                    }
                }

                //获取关联
                RetrieveRelations(mgr, cmd, list);

                /*------------------------------数据库SQL操作结束----------------------------------*/
            });

            return list;
        }

        public override int GetCount(TransactionManager manager, string whereClause)
        {
            int count = 0;
            Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
            {
                /*------------------------------数据库SQL操作开始----------------------------------*/
                //获取SQL语句
                cmd.CommandText = String.Format("select count(*) from tb_chemistry_immunity {0}",
                    ProcessWhereClause(whereClause));

                //执行获取实体集数量
                count = Convert.ToInt32(cmd.ExecuteScalar());

                /*------------------------------数据库SQL操作结束----------------------------------*/
            });

            return count;
        }

        public override long GetVersion(TransactionManager manager, string whereClause)
        {
            long count = 0;
            DateTime? timeStamp = null;
            Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
            {
                /*------------------------------数据库SQL操作开始----------------------------------*/
                //获取SQL语句
                cmd.CommandText = String.Format("select count(*), max(time_stamp) from tb_chemistry_immunity {0}",
                    ProcessWhereClause(whereClause));

                //执行获取实体集数量
                using (IDataReader reader = cmd.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        count = reader.GetInt64(0);
                        timeStamp = reader.IsDBNull(1) ? (DateTime?)null : reader.GetDateTime(1);
                    }
                }

                /*------------------------------数据库SQL操作结束----------------------------------*/
            });

            return (timeStamp ?? DateTime.MinValue).Ticks ^ count;
        }

        public override ChemistryImmunity Fill(IDataReader reader, ChemistryImmunity entity)
        {
            if (entity == null) entity = new ChemistryImmunity();

            for (int i = 0; i < reader.FieldCount; i++)
            {
                switch (reader.GetName(i))
                {
                    case "id":
                        entity.Id = reader.GetInt64(i);
                        break;
                    case "IL2":
                        entity.Il2 = reader[i] == DBNull.Value ? entity.Il2 : reader.GetDecimal(i);
                        break;
                    case "IL6":
                        entity.Il6 = reader[i] == DBNull.Value ? entity.Il6 : reader.GetDecimal(i);
                        break;
                    case "IL8":
                        entity.Il8 = reader[i] == DBNull.Value ? entity.Il8 : reader.GetDecimal(i);
                        break;
                    case "IL10":
                        entity.Il10 = reader[i] == DBNull.Value ? entity.Il10 : reader.GetDecimal(i);
                        break;
                    case "TNF":
                        entity.Tnf = reader[i] == DBNull.Value ? entity.Tnf : reader.GetDecimal(i);
                        break;
                    case "CRP":
                        entity.Crp = reader[i] == DBNull.Value ? entity.Crp : reader.GetDecimal(i);
                        break;
                    case "neomycin":
                        entity.Neomycin = reader[i] == DBNull.Value ? entity.Neomycin : reader.GetDecimal(i);
                        break;
                    case "comment":
                        entity.Comment = reader[i] == DBNull.Value ? entity.Comment : reader.GetString(i);
                        break;
                    case "time_stamp":
                        entity.TimeStamp = reader.GetDateTime(i);
                        break;
                }
            }

            entity.EntityState = Stone.Components.EntityState.Saved;
            return entity;
        }

        #region 实体关联操作数据提供类

        #endregion
    }
}
